package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程商品表 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/api/shopping")
public class ShoppingController {

    @Autowired
            @Lazy
    IShoppingService iShoppingService;

    @Autowired
    @Lazy
    IShoppingDiscountsService iShoppingDiscountsService;

    @Autowired
    @Lazy
    ICourseService iCourseService;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    @Lazy
    IOrganizationService iOrganizationService;

    @Autowired
    IdWorker idWorker;

    @GetMapping()
    public Result findAllPage(@RequestParam(value = "schoolId",defaultValue = "")String schoolId,
                          @RequestParam(value = "gradeId",defaultValue = "")String gradeId,
                          @RequestParam(value = "status",defaultValue = "")String status,
                          @RequestParam(value = "yearClassId")String yearClassId,
                          @RequestParam(value = "keyword",defaultValue = "")String keyword,
                          @RequestParam(value = "pageNum")Long pageNum,
                          @RequestParam(value = "pageSize")Long pageSize){



        QueryWrapper<Shopping> shoppingQueryWrapper = new QueryWrapper<>();
        shoppingQueryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        shoppingQueryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        shoppingQueryWrapper.eq(StringUtils.isNotBlank(yearClassId),"year_class_id",yearClassId);
        shoppingQueryWrapper.eq(StringUtils.isNotBlank(status),"status",status);
        shoppingQueryWrapper.like(StringUtils.isNotBlank(keyword),"name",keyword);


        IPage<Shopping> iPage = new Page<Shopping>(pageNum,pageSize);
        iShoppingService.page(iPage,shoppingQueryWrapper);
        for (Shopping shopping:iPage.getRecords()){
            Course course = iCourseService.getById(shopping.getCourseId());
            if (course != null)
                shopping.setCourseName(course.getName());
            Grade grade = iGradeService.getById(shopping.getGradeId());
            if (grade != null)
                shopping.setGradeName(grade.getName());
            Organization organization = iOrganizationService.getById(shopping.getSchoolId());
            if (organization != null)
                shopping.setShoolName(organization.getName());
        }
        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }

    @PostMapping()
    public Result save(@RequestBody Shopping shopping){
        Long id = idWorker.nextId();
        shopping.setId(id);
        Grade grade = iGradeService.getById(shopping.getGradeId());
        Course course = iCourseService.getById(shopping.getCourseId());
        shopping.setYearPart(grade.getYearPart());
        shopping.setCourseId(course.getId());
        shopping.setYearClassId(grade.getYearClassId().getKey());
        shopping.setQuarterNum(course.getQuarterNum());
        boolean rs = iShoppingService.save(shopping);
        iShoppingDiscountsService.remove(new QueryWrapper<ShoppingDiscounts>().eq("shopping_id",shopping.getId()));
        for (ShoppingDiscounts shoppingDiscounts:shopping.getShoppingDiscountsList()){
            shoppingDiscounts.setShoppingId(shopping.getId());
            shoppingDiscounts.setShoppingId(shopping.getSchoolId());
            iShoppingDiscountsService.save(shoppingDiscounts);
        }
        return new Result(rs? Code.OK:Code.ERROR);
    }

    @PutMapping()
    public Result update(@RequestBody Shopping shopping){
        //Long id = idWorker.nextId();
        Grade grade = iGradeService.getById(shopping.getGradeId());
        Course course = iCourseService.getById(shopping.getCourseId());
        shopping.setYearPart(grade.getYearPart());
        shopping.setCourseId(course.getId());
        shopping.setYearClassId(grade.getYearClassId().getKey());

        boolean rs = iShoppingService.save(shopping);
        iShoppingDiscountsService.remove(new QueryWrapper<ShoppingDiscounts>().eq("shopping_id",shopping.getId()));
        for (ShoppingDiscounts shoppingDiscounts:shopping.getShoppingDiscountsList()){
            shoppingDiscounts.setShoppingId(shopping.getId());
            shoppingDiscounts.setShoppingId(shopping.getSchoolId());
            iShoppingDiscountsService.save(shoppingDiscounts);
        }
        return new Result(rs? Code.OK:Code.ERROR);
    }

    @DeleteMapping()
    @Transactional
    public Result deleted(@RequestBody Shopping shopping){
        iShoppingDiscountsService.remove(new QueryWrapper<ShoppingDiscounts>().eq("shopping_id",shopping.getId()));
        boolean rs = iShoppingService.removeById(shopping.getId());
        return new Result(rs? Code.OK:Code.ERROR);
    }
}
